/*
 * jQuey Notify Plugin:
 * Author: Anas Nakawa
 * License: MIT
 */
(function ($) {
    $.extend({
		_timeout: null,
        jNotify: function (options) {
            var defaults = {
                type: 'confirm', // confirm, error, info, warn
                message: 'Welcome to jNotify',
				delay: 5000,
				open: null,
				close: null,
				beforeOpen: null,
				beforeClose: null,
				id: 'NotifyBox'
            },
            options = $.extend(defaults, options),
			$notify = $('#' + options.id);
			if($notify.size()) {
				clearTimeout($._timeout); 
				$notify.slideUp(function(){
					var $this = $(this);
					$this.attr('class', options.type);
					$this.find('ul').html($._generateMessages(options.message));
				});
			}
			$('body').append(function(){
				return '<div id="' + options.id + '" class="' + options.type + '"><span><ul>' + $._generateMessages(options.message) + '</ul></span></div>';
			});
			$notify = $('#' + options.id);
			$notify.slideDown(function(){
				if(options.open) {
					options.open();
				}
			}).click(function(){
				$(this).fadeOut(function(){
					if(options.close) {
						options.close();
					}
				});
			});
			
			if(options.delay && !isNaN(options.delay)) {
				$._timeout = setTimeout(function(){
					$notify.slideUp(function(){
						if(options.close) {
							options.close();
						}
					});
				}, options.delay);
			}
			return;
        },
		// private method to generate messages
		_generateMessages: function(message) {
			var msg = '';
			// if object is array
			if((typeof message == 'object') && !isNaN(message.length) && message.length != 1) {
				for(var n in message) {
					msg += '<li>' + message[n] + '</li>';
				}
			} else if(typeof message == 'string') {
				msg = message;
			} else if(message.length == 1) {
				msg = message[0];
			}
			return msg;
		}
    });
})(jQuery);